package com.beijiteshop.shop.ui.detail.vm

import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.network.ext.onBizEmpty
import com.beijiteshop.libcommon.network.ext.onBizError
import com.beijiteshop.libcommon.network.ext.onBizOK
import com.beijiteshop.libcommon.network.ext.serverData
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.shop.model.api.response.ProductDetailBean
import com.beijiteshop.shop.model.entity.ProductDetailItemEntity
import com.beijiteshop.shop.ui.detail.repo.ProductDetailRepo

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/8 15:38
 * @Description
 */
class ProductDetailVM(private val repo: ProductDetailRepo) : BaseViewModel() {

    val liveProductDetail = MutableLiveData<ProductDetailBean>()

    fun getProductDetailInfo(goodId: String) = launch {
        repo.getProductDetailInfo(goodId)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    liveProductDetail.postValue(data)
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }

                onBizEmpty {
                    liveEmpty.postValue(it)
                }
            }.onFailure {
                liveNetError.postValue(it.message)
            }
    }

    /**
     * 获取商品详情信息
     */
    fun getProductDetailItemBean(): MutableList<ProductDetailItemEntity>{
        val list = mutableListOf<ProductDetailItemEntity>()
        list.add(ProductDetailItemEntity(ProductDetailItemEntity.TYPE_HEADER_TITLE,2,"--用户评价--"))
//        for (i in 0..10){
//            list.add(ProductDetailItemEntity(ProductDetailItemEntity.TYPE_EVALUATION,2,EvaluationBean("骑蜗牛闯红灯$i","极好","味道针不戳","2021-03-04 19:25:56")))
//        }
        list.add(ProductDetailItemEntity(ProductDetailItemEntity.TYPE_HEADER_TITLE,2,"--精选好物--"))
        return list
    }
}